'use strict';

angular.module('hackathonApp')
  .controller('DunsCtrl', function ($scope, $routeParams) {	  
	  
	  $scope.lookupDUNS = function() {		  
		  if($scope.state) {
		      $scope.DNB.Api.get('V4.0/organizations?TerritoryName-1=' + $scope.state + '&CountryISOAlpha2Code-1=US&KeywordText=' + $scope.company_name + '&SearchModeDescription=Advanced&findcompany=true').done(function(response) {
				  if (typeof(response.FindCompanyResponse) != 'undefined' && response.FindCompanyResponse.FindCompanyResponseDetail.CandidateReturnedQuantity) {
				  	$scope.companies = response.FindCompanyResponse.FindCompanyResponseDetail.FindCandidate;
//					console.log($scope.companies);
					/*for(var obj in $scope.companies){
						var comp_name = $scope.companies[obj].OrganizationPrimaryName.OrganizationName.$;
						var street = $scope.companies[obj].PrimaryAddress.StreetAddressLine[0].LineText;
						var town =$scope.companies[obj].PrimaryAddress.PrimaryTownName;
						var state =encodeURIComponent($scope.companies[obj].PrimaryAddress.TerritoryAbbreviatedName);
						var comp_name =$scope.companies[obj].PrimaryAddress.CountryISOAlpha2Code;
						var fraud_url = 'V4.0/organizations?TerritoryName='+encodeURIComponent(state)+'&CountryISOAlpha2Code=US&StreetAddressLine-1='+encodeURIComponent(street)+'&PrimaryTownName='+encodeURIComponent(town)+'&SubjectName='+encodeURIComponent(comp_name)+'&fraudscore=true'
						$scope.DNB.Api.get(fraud_url).done(function (response) {
							//place holder, use as approriate.
							console.log(response);				
						});
					}*/
				  	$scope.$apply();
			      }
			  });		  	
		  } else {
			  console.log($scope.company_name);
			  $scope.DNB.Api.get('V4.0/organizations?KeywordText='+ $scope.company_name +'&SearchModeDescription=Advanced&findcompany=true').done(function (response){
				  console.log(response);
				  if (typeof(response.FindCompanyResponse) != 'undefined' && response.FindCompanyResponse.FindCompanyResponseDetail.CandidateReturnedQuantity) {
					$scope.companies = response.FindCompanyResponse.FindCompanyResponseDetail.FindCandidate;
					/*for(var obj in $scope.companies){
						var comp_name = $scope.companies[obj].OrganizationPrimaryName.OrganizationName.$;
						var street = $scope.companies[obj].PrimaryAddress.StreetAddressLine[0].LineText;
						var town =$scope.companies[obj].PrimaryAddress.PrimaryTownName;
						var state =encodeURIComponent($scope.companies[obj].PrimaryAddress.TerritoryAbbreviatedName);
						var comp_name =$scope.companies[obj].PrimaryAddress.CountryISOAlpha2Code;
						var fraud_url = 'V4.0/organizations?TerritoryName='+encodeURIComponent(state)+'&CountryISOAlpha2Code=US&StreetAddressLine-1='+encodeURIComponent(street)+'&PrimaryTownName='+encodeURIComponent(town)+'&SubjectName='+encodeURIComponent(comp_name)+'&fraudscore=true'
						$scope.DNB.Api.get(fraud_url).done(function (response) {
							//place holder, use as approriate.
							console.log(response);				
						});
					}*/
				  }
				  $scope.$apply();
			  	});
		  }
	  }	  
		
	/*	// Search by name
		$scope.DNB.Api.get('V2.1/organizations/362241452/products/DCP_ENH').done(function (response) {
//			console.log(response);
			$scope.company_detail = response;
		});

		// Get Predict Bankrupcy & Payment Risk Enhanced
		$scope.DNB.Api.get('V2.1/organizations/362241452/products/PBPR_STD').done(function (response) {
			$scope.company_brpr = response;
//			console.log(response);
		});
		
		// FraudScore Search based on parameters, no DUNS available.
		$scope.DNB.Api.get('V4.0/organizations?TerritoryName=MD&CountryISOAlpha2Code=US&StreetAddressLine-1=403%20600%20S%20Frederick Ave&PrimaryTownName=Gaithersburg&SubjectName=Dsfederal&fraudscore=true').done(function (response) {
	//		console.log(response);
			$scope.company_fraud = response;
		});*/
		
  	  if($routeParams.name || $routeParams.state) {
  		  if($routeParams.name) {
  			  $scope.company_name = $routeParams.name;
  		  }
  		  if($routeParams.state) {
  			  $scope.state = $routeParams.state;
  		  }
  		  $scope.lookupDUNS();
  	  }
		
		

  }).directive('tabs', function(){
    return {
      restrict: 'E',
      transclude: true,
      scope: {},
      controller: function($scope, $element) {
        var panes = $scope.panes = [];
 
        $scope.select = function(pane) {
          angular.forEach(panes, function(pane) {
            pane.selected = false;
          });
          pane.selected = true;
        }
 
        this.addPane = function(pane) {
          if (panes.length == 0) $scope.select(pane);
          panes.push(pane);
        }
      },
      template:
        '<div class="tabbable">' +
          '<ul class="nav nav-tabs">' +
            '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
              '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
            '</li>' +
          '</ul>' +
          '<div class="tab-content" ng-transclude></div>' +
        '</div>',
      replace: true
    };
  }).directive('pane', function() {
    return {
      require: '^tabs',
      restrict: 'E',
      transclude: true,
      scope: { title: '@' },
      link: function(scope, element, attrs, tabsCtrl) {
        tabsCtrl.addPane(scope);
      },
      template:
        '<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
        '</div>',
      replace: true
    };
});